package service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import bean.CustomUser;

@Service
public class CustomUserDetailService implements UserDetailsService{

    public UserDetails loadUserByUsername(String username)throws UsernameNotFoundException {  
        try {  
           /* CustomUser userFromQuery = jdbcTemplate.queryForObject(sqlLoadUser,  
                    myUserDetailsRowMapper, username, username, username);  
         
            List<GrantedAuthority> authorities = jdbcTemplate.query(  
                    sqlLoadAuthorities, authorityRowMapper, username);  
            return new CustomUser(userFromQuery.getId(), userFromQuery.getUsername(),  
                    userFromQuery.getPassword(), userFromQuery.isEnabled(),  
                    authorities);  */
        
        	//模拟返回用户信息，此处可以使用JDBC从数据库获取用户信息。
        	GrantedAuthority au = new SimpleGrantedAuthority("ROLE_USER"); 
        	List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        	authorities.add(au);
        	return new CustomUser(Long.valueOf(1), "chen", "AAA", true, authorities);
        	
        } catch (EmptyResultDataAccessException e) {  
            throw new UsernameNotFoundException("用户名或密码不正确");  
        }  
    }  
 /*   @Autowired  
    JdbcTemplate jdbcTemplate;  
    private final String sqlLoadUser;  
    private final String sqlLoadAuthorities;  
    private final RowMapper<CustomUser> myUserDetailsRowMapper;  
    private final RowMapper<GrantedAuthority> authorityRowMapper;  */
  
  
    public CustomUserDetailService() {  
    	 /*   super();  
       sqlLoadUser = "SELECT id,username,password,enabled FROM user WHERE username=? OR phoneNumber=? OR email=?";  
        sqlLoadAuthorities = "SELECT authority FROM view_role WHERE username=?";  
      
        myUserDetailsRowMapper = new RowMapper<CustomUser>() {  
            public CustomUser mapRow(ResultSet rs, int rowNum) throws SQLException {  
                return new CustomUser(rs.getLong(1), rs.getString(2),  
                        rs.getString(3), rs.getBoolean(4));  
            }  
        };  
     
        authorityRowMapper = new RowMapper<GrantedAuthority>() {  
   
            public GrantedAuthority mapRow(ResultSet rs, int rowNum)  
                    throws SQLException {  
                return new SimpleGrantedAuthority(rs.getString(1));  
            }  
        };  */
    }  
  
}
